<?php
/**
 *   OPENSHOP 尺码属性相关设置
 * 
 *   @link        http://baison.com.cn
 *   @copyright   Baison, Inc.
 *   @package     OpenShop
 *   @version     $Id: tb_size.php,v 1.0 2009/11/12 07:35:58 modified $
 *   @author      winner <jsycywm@hotmail.com>
 */

define('IN_OS', true);

require_once(dirname(__FILE__).'/includes/init.php');
$exc = new exchange($os->table('tb_guige2'),$db,'id','vid');

/* act操作项的初始化 */
if (empty($_REQUEST['act'])){
    $_REQUEST['act'] = 'list';
}else{
    $_REQUEST['act'] = trim($_REQUEST['act']);
}

if($_REQUEST['act']=='list')
{	
	//检查权限
	admin_priv(); 
	$smarty->assign('ur_here',      '淘宝宝贝尺码关联');   
	$smarty->assign('full_page',    1);
	$size_list = get_tb_sizelist(); 
	
	$smarty->assign('size_list',    $size_list['size']);
    $smarty->assign('filter',       $size_list['filter']);
    $smarty->assign('record_count', $size_list['record_count']);
    $smarty->assign('page_count',   $size_list['page_count']);	 
    assign_query_info();
	$smarty->display('api_tb_size_list.tpl');	
}  
elseif ($_REQUEST['act'] == 'query')
{
    $size_list = get_tb_sizelist();  
    $smarty->assign('size_list',    $size_list['size']);
    $smarty->assign('filter',       $size_list['filter']);
    $smarty->assign('record_count', $size_list['record_count']);
    
    $smarty->assign('page_count',   $size_list['page_count']); 
    make_json_result($smarty->fetch('api_tb_size_list.tpl'), '',
    array('filter' => $size_list['filter'], 'page_count' => $size_list['page_count']));
}
elseif($_REQUEST['act']=='relevancy')
{  
	$smarty->assign('ur_here',      '淘宝宝贝尺码关联设置'); 
	$smarty->assign('action_link',  array('href' => 'api_tb_size.php?act=list', 'text' => '淘宝宝贝尺码管理')); 
	$vid =  intval($_GET['vid'] ) ; 

	$cid =  intval($_GET['cid'] ) ;
	$tb_size_name = $db->getOne("SELECT name FROM ".$os->table('tb_guige2')." WHERE vid='".$vid."'"); 
	$itemcat_name = $db->getOne("SELECT name FROM ".$os->table('tb_itemcat')." WHERE cid='".$cid."'"); 
	$smarty->assign('cid',$cid); 
	$smarty->assign('cname','['.$cid.']'.$itemcat_name);  
	$smarty->assign('cat_size_list',get_tb_cat_sizelist( $vid,$cid) );  
	$smarty->assign('vid',$vid);  
	$smarty->assign('vname','['.$vid.']'.$tb_size_name);  
	
	assign_query_info(); 
	$smarty->display('api_tb_size_relevancy.tpl');	
}elseif ($_REQUEST['act']=='search_cat'){
    /* 检查权限 */  
    include_once(ROOT_PATH . 'includes/cls_json.php'); 
    $json   = new JSON;
    $filter = $json->decode($_GET['JSON']);
    $filter->keyword = json_str_iconv($filter->keyword);
    $sql = "SELECT size_code AS id, CONCAT('[',size_code,']', size_name) as name FROM " . $os->table('size') .
        " WHERE size_name LIKE '%" . mysql_like_quote($filter->keyword) . "%' or size_code LIKE '%" . mysql_like_quote($filter->keyword) . "%' LIMIT 50";
   $arr = $db->getAll($sql);  
    if (empty($arr))
    {
        $arr = array(0 => array(
            'id'   => 0,
            'name' => '没有找到相应记录，请重新搜索'
        ));
    } 
    make_json_result($arr);
}elseif ($_REQUEST['act']=='add_cat_size'){ 
    include_once(ROOT_PATH . 'includes/cls_json.php'); 
    $json = new JSON; 
    $size_code =$json->decode(  $_GET['add_ids'] );
    $size_code =$size_code[0] ; 
    $arguments  = $json->decode($_GET['JSON']);
    $vid   = $arguments[0];  
    $cid   = $arguments[1];  
    $sql = "update " . $os->table('tb_guige2') . " set size_code='".$size_code."'where vid=". $vid;
	$db->query($sql);   
    $arr = get_tb_cat_sizelist($vid,$cid);
    $opt = array(); 
    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['size_code'],
                        'text'      => '['.$val['size_code'].']'.$val['size_name'],
                        'data'      => ''); 
    } 
    clear_cache_files();
    make_json_result($opt);
}elseif($_REQUEST['act']=='drop_cat_size'){
	 /* 检查权限 */
    check_authz_json('relevancy_cat_size'); 
    include_once(ROOT_PATH . 'includes/cls_json.php'); 
    $json = new JSON; 
    $size_code =$json->decode(  $_GET['add_ids'] );
	$size_code =$size_code[0] ; 
	$arguments  = $json->decode($_GET['JSON']);
	$vid   = $arguments[0];
	$cid   = $arguments[1];
    $sql = "update " . $os->table('tb_guige2') . " set size_code='' where vid=". $vid; 
    $db->query($sql);  
    $arr = get_tb_cat_sizelist($vid,$cid);
    $opt = array(); 
    foreach ($arr AS $val)
    {
        $opt[] = array('value'      => $val['cat_id'],
                       'text'       => $val['cat_name'],
                       'data'       => '');
    }
    
    clear_cache_files();
    make_json_result($opt);	
}elseif($_REQUEST['act']=='auto_relevancy'){
	$sql = "update " . $os->table('tb_guige2') . " as tb_c,".$os->table('size')." as c set tb_c.size_code=c.size_code   where tb_c.vid=c.vid;"; 
	$db->query($sql); 
	
	$link[0]['text'] = '尺码关联';
	$link[0]['href'] = 'api_tb_size.php?act=list';
	$link[0]['flag'] = 'api_tb_size';
	sys_msg('关联成功！', 0, $link);
	clear_cache_files();
	
}

function get_tb_sizelist(){
    $result = get_filter();
    if ($result === false)
    {
        /* 分页大小 */
        $filter = array();

        $filter['keyword']  = empty($_REQUEST['keyword']) ? '' : trim($_REQUEST['keyword']);

        if ($_REQUEST['is_ajax'] == 1)
        {
            $filter['keyword'] = json_str_iconv($filter['keyword']);
        }        
        
        if ($filter['keyword'])
        {
			$where .= " AND (tb_gg2.vid LIKE '%" . mysql_like_quote($filter['keyword']) . "%' 
					OR tb_gg2.name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
					OR tb_gg2.cid LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
				    OR tb_ic.name LIKE '%" . mysql_like_quote($filter['keyword']) . "%'
					)"; 
        }         
        
        /* 记录总数以及页数 */
        $sql = "SELECT COUNT(*) FROM ".$GLOBALS['os']->table('tb_guige2').' as tb_gg2
        	   left join open_tb_itemcat as tb_ic on tb_ic.cid =tb_gg2.cid
        	   where 1 '.$where;
        $filter['record_count'] = $GLOBALS['db']->getOne($sql);

        $filter = page_and_size($filter);     
        
		$sql = "select tb_gg2.vid, tb_gg2.cid,tb_ic.name as icname ,tb_gg2.pid,tb_gg2.prop_name,tb_gg2.name,tb_gg2.size_code ,s.size_name
				from open_tb_guige2 as tb_gg2 left join open_tb_itemcat as tb_ic on tb_gg2.cid =tb_ic.cid 
				left join open_size as s on tb_gg2.size_code =s.size_code  where 1 ".$where." ORDER BY  tb_gg2.id DESC";  
		
        set_filter($filter, $sql);
    }
    else
    {
        $sql    = $result['sql'];
        $filter = $result['filter'];
    }
    $res = $GLOBALS['db']->selectLimit($sql, $filter['page_size'], $filter['start']);
    
    $arr = array();
    while ($rows = $GLOBALS['db']->fetchRow($res))
    {
        $arr[] = $rows;
    }	
    
    return array('size' => $arr, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);	
}

function get_tb_cat_sizelist($vid=0,$cid=0){
	static $rows = NULL;  
    $sql ="select tb_gg2.cid,tb_itemcat.name,tb_gg2.vid,tb_gg2.size_code,s.size_name from open_tb_guige2 as tb_gg2 
			left join open_size as  s  on s.size_code =tb_gg2.size_code
			left join open_tb_itemcat as tb_itemcat on tb_itemcat.cid =tb_gg2.cid ";  

	if($vid>0){ 
		$sql .= " WHERE tb_gg2.vid = '$vid' and tb_gg2.cid = '$cid' ";
	}
	$sql .= " ORDER BY tb_gg2.id DESC";  
	if ($rows === NULL){
		$rows = $GLOBALS['db']->getAll($sql);
	}
	return $rows;	 
}
?>